package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.*;
import com.qf.model.pojo.Userloans;
import com.qf.service.LoansService;
import com.qf.service.impl.LoansServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/LoansServlet")
public class LoansServlet extends BascServlet {
    private LoansService loansService=new LoansServiceImpl();
    //查询所有贷款产品
    public  void finAll(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        List<Loans> list = loansService.findAll();
        Massage<List<Loans>> m=new Massage<>(1,"查询成功",list);
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }
    //根据u_id查询贷款信息
    public void loans(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        String uid = req.getParameter("u_id"); //用户id
        String lid = req.getParameter("l_id");  //产品编号
        double quota = Double.parseDouble(req.getParameter("quota"));   //贷款额度
        String cid = req.getParameter("c_id");     //还款账号
        String uidcard = req.getParameter("u_idcard");      //贷款/还款人身份证号

        int i = loansService.addMyLoans(Integer.parseInt(lid), Integer.parseInt(uid), quota, cid);
        Massage massage = new Massage();
        if (i==0){
            massage.setCode(-1);
            massage.setMsg("处理失败");
        }else {
            massage.setCode(1);
            massage.setMsg("贷款申请提交成功");
        }
        //拿到转账详情，用json返回给前端页面
        String s=JSON.toJSONString(massage);
        resp.getWriter().write(s);

    }
    //管理员查询所有待审批贷款
    public  void  findAllMyLoans(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //page:page,pagesize:pagesize
        int page = Integer.parseInt(req.getParameter("page"));
        int pagesize =Integer.parseInt( req.getParameter("pagesize"));

        PageBean<Userloans> pageBean=new PageBean<>();
        pageBean.setCurPage(page);
        pageBean.setPageSize(pagesize);
        List<Userloans> allMyLoans = loansService.findAllMyLoans(pageBean);
        pageBean.setData(allMyLoans);

        if(pageBean.getCount()%pageBean.getPageSize()==0){
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize());
        }else {
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize()+1);
        }
        Massage<PageBean<Userloans>> m=new Massage<>(1,"查询成功",pageBean);
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //管理员 审批 贷款信息   拒绝 贷款信息
    public  void sUpdateMyLoans(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        int l_id = Integer.parseInt(req.getParameter("l_id"));
        int type = Integer.parseInt(req.getParameter("type"));
        //type:1:审批，2：拒绝
        Massage m=null;
        if(type==1){
            //审批
            //验证贷款平台余额是否充足
            //1.根据贷款id获取贷款信息
            MyLoans myLoans = loansService.findMyLoans(l_id);
            if(myLoans!=null){
                //2.根据贷款平台id查询贷款产品信息
                Loans loans = loansService.findLoans(myLoans.getL_id());
                //3.判断贷款平台余额是否充足
                if(loans.getMoney()>myLoans.getQuota()){
                    //4.执行审批功能，并发放，扣款，添加日志
                    int i = loansService.UpdateMyLoans(myLoans.getId());
                    if(i==1){
                        m=new Massage(1,"审批成功");
                    }else {
                        m=new Massage(-1,"审批失败");
                    }
                }else {
                    m=new Massage(-1,"贷款平台余额不足，不可审批");
                }
            }else {
                m=new Massage(-1,"贷款产品已下架，不可贷款");
            }
        }else {
            //拒绝
            int i = loansService.sUpdateMyLoans(l_id);
            if(i==1){
                m=new Massage(1,"拒绝成功");
            }else {
                m=new Massage(-1,"拒绝失败");
            }
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }
}
